

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Arrows and other pointy sticky things &mdash; pymunk 3.0.0 documentation</title>
    
    <link rel="stylesheet" href="../_static/pymunk.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '3.0.0',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <link rel="shortcut icon" href="../_static/pymunk_favicon.ico"/>
    <link rel="top" title="pymunk 3.0.0 documentation" href="../index.html" />
    <link rel="up" title="Tutorials" href="../tutorials.html" />
    <link rel="next" title="Slide and Pin Joint Demo Step by Step" href="SlideAndPinJoint.html" />
    <link rel="prev" title="Tutorials" href="../tutorials.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="SlideAndPinJoint.html" title="Slide and Pin Joint Demo Step by Step"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="../tutorials.html" title="Tutorials"
             accesskey="P">previous</a> |</li>
        <li><a href="../index.html">pymunk 3.0.0 documentation</a> &raquo;</li>
          <li><a href="../tutorials.html" accesskey="U">Tutorials</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="arrows-and-other-pointy-sticky-things">
<h1>Arrows and other pointy sticky things<a class="headerlink" href="#arrows-and-other-pointy-sticky-things" title="Permalink to this headline">¶</a></h1>
<div class="admonition attention">
<p class="first admonition-title">Attention</p>
<p class="last">This tutorial is work in progress. Take a look at the arrows.py example file
in <a class="reference internal" href="../examples.html#id1"><em>Examples</em></a> for fully working code for the arrows.</p>
</div>
<p>This tutorial will explain one way to make arrows/sticky projectiles that can stick on the target.</p>
<p>The tutorial is heavily inspired by the Box2d tutorial &#8220;Box2D C++ tutorials - Sticky projectiles&#8221; found
here <a class="reference external" href="http://www.iforce2d.net/b2dtut/sticky-projectiles">http://www.iforce2d.net/b2dtut/sticky-projectiles</a> (but adjusted for python, pymunk and chipmunk).</p>
<div class="section" id="before-we-start">
<h2>Before we start<a class="headerlink" href="#before-we-start" title="Permalink to this headline">¶</a></h2>
<p>For this tutorial you will need to know some pymunk basics. I recommend that you read the other tutorial(s)
and try out easier examples first before you continue.</p>
<p>Except for pymunk you will also need pygame to follow this tutorial. However, it should be no problem to
use another graphics and input library if you want, for example pyglet.</p>
<p>We will try to accomplish</p>
<ul class="simple">
<li>An arrow that flies believable in the air</li>
<li>Figure out when the arrow hits something and should stick</li>
<li>Attach the arrow to an object when hit</li>
</ul>
<p>In the end we should have a cannon like object shooting arrows that flies in a believable way and sticks to
objects if they hit hard enough.</p>
</div>
<div class="section" id="basic-scene">
<h2>Basic scene<a class="headerlink" href="#basic-scene" title="Permalink to this headline">¶</a></h2>
<p>Before we start with the arrow we need a scene to contain it and a &#8220;cannon&#8221; that can aim:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">sys</span>

<span class="kn">import</span> <span class="nn">pygame</span>
<span class="kn">from</span> <span class="nn">pygame.locals</span> <span class="kn">import</span> <span class="o">*</span>
<span class="kn">from</span> <span class="nn">pygame.color</span> <span class="kn">import</span> <span class="o">*</span>

<span class="kn">import</span> <span class="nn">pymunk</span>
<span class="kn">from</span> <span class="nn">pymunk.vec2d</span> <span class="kn">import</span> <span class="n">Vec2d</span>
<span class="kn">from</span> <span class="nn">pymunk.pygame_draw</span> <span class="kn">import</span> <span class="n">draw_space</span><span class="p">,</span> <span class="n">from_pygame</span>

<span class="n">width</span> <span class="o">=</span> <span class="n">height</span> <span class="o">=</span> <span class="mi">600</span>
<span class="k">def</span> <span class="nf">main</span><span class="p">():</span>
    <span class="c">### PyGame init</span>
    <span class="n">pygame</span><span class="o">.</span><span class="n">init</span><span class="p">()</span>
    <span class="n">screen</span> <span class="o">=</span> <span class="n">pygame</span><span class="o">.</span><span class="n">display</span><span class="o">.</span><span class="n">set_mode</span><span class="p">((</span><span class="n">width</span><span class="p">,</span><span class="n">height</span><span class="p">))</span>
    <span class="n">clock</span> <span class="o">=</span> <span class="n">pygame</span><span class="o">.</span><span class="n">time</span><span class="o">.</span><span class="n">Clock</span><span class="p">()</span>
    <span class="n">running</span> <span class="o">=</span> <span class="bp">True</span>
    <span class="n">font</span> <span class="o">=</span> <span class="n">pygame</span><span class="o">.</span><span class="n">font</span><span class="o">.</span><span class="n">SysFont</span><span class="p">(</span><span class="s">&quot;Arial&quot;</span><span class="p">,</span> <span class="mi">16</span><span class="p">)</span>

    <span class="c">### Physics stuff</span>
    <span class="n">space</span> <span class="o">=</span> <span class="n">pymunk</span><span class="o">.</span><span class="n">Space</span><span class="p">()</span>

    <span class="c"># walls - the left-top-right-bottom walls</span>
    <span class="n">static_lines</span> <span class="o">=</span> <span class="p">[</span><span class="n">pymunk</span><span class="o">.</span><span class="n">Segment</span><span class="p">(</span><span class="n">space</span><span class="o">.</span><span class="n">static_body</span><span class="p">,</span> <span class="p">(</span><span class="mi">50</span><span class="p">,</span> <span class="mi">50</span><span class="p">),</span> <span class="p">(</span><span class="mi">50</span><span class="p">,</span> <span class="mi">550</span><span class="p">),</span> <span class="mi">5</span><span class="p">)</span>
                <span class="p">,</span><span class="n">pymunk</span><span class="o">.</span><span class="n">Segment</span><span class="p">(</span><span class="n">space</span><span class="o">.</span><span class="n">static_body</span><span class="p">,</span> <span class="p">(</span><span class="mi">50</span><span class="p">,</span> <span class="mi">550</span><span class="p">),</span> <span class="p">(</span><span class="mi">550</span><span class="p">,</span> <span class="mi">550</span><span class="p">),</span> <span class="mi">5</span><span class="p">)</span>
                <span class="p">,</span><span class="n">pymunk</span><span class="o">.</span><span class="n">Segment</span><span class="p">(</span><span class="n">space</span><span class="o">.</span><span class="n">static_body</span><span class="p">,</span> <span class="p">(</span><span class="mi">550</span><span class="p">,</span> <span class="mi">550</span><span class="p">),</span> <span class="p">(</span><span class="mi">550</span><span class="p">,</span> <span class="mi">50</span><span class="p">),</span> <span class="mi">5</span><span class="p">)</span>
                <span class="p">,</span><span class="n">pymunk</span><span class="o">.</span><span class="n">Segment</span><span class="p">(</span><span class="n">space</span><span class="o">.</span><span class="n">static_body</span><span class="p">,</span> <span class="p">(</span><span class="mi">50</span><span class="p">,</span> <span class="mi">50</span><span class="p">),</span> <span class="p">(</span><span class="mi">550</span><span class="p">,</span> <span class="mi">50</span><span class="p">),</span> <span class="mi">5</span><span class="p">)</span>
                <span class="p">]</span>
    <span class="n">space</span><span class="o">.</span><span class="n">add_static</span><span class="p">(</span><span class="n">static_lines</span><span class="p">)</span>

    <span class="c">### &quot;Cannon&quot; that can fire arrows</span>
    <span class="n">cannon_body</span> <span class="o">=</span> <span class="n">pymunk</span><span class="o">.</span><span class="n">Body</span><span class="p">()</span>
    <span class="n">player_shape</span> <span class="o">=</span> <span class="n">pymunk</span><span class="o">.</span><span class="n">Circle</span><span class="p">(</span><span class="n">cannon_body</span><span class="p">,</span> <span class="mi">25</span><span class="p">)</span>
    <span class="n">cannon_body</span><span class="o">.</span><span class="n">position</span> <span class="o">=</span> <span class="mi">100</span><span class="p">,</span><span class="mi">100</span>

    <span class="n">space</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">player_shape</span><span class="p">)</span>

    <span class="k">while</span> <span class="n">running</span><span class="p">:</span>
        <span class="k">for</span> <span class="n">event</span> <span class="ow">in</span> <span class="n">pygame</span><span class="o">.</span><span class="n">event</span><span class="o">.</span><span class="n">get</span><span class="p">():</span>
            <span class="k">if</span> <span class="n">event</span><span class="o">.</span><span class="n">type</span> <span class="o">==</span> <span class="n">QUIT</span> <span class="ow">or</span> \
                <span class="n">event</span><span class="o">.</span><span class="n">type</span> <span class="o">==</span> <span class="n">KEYDOWN</span> <span class="ow">and</span> <span class="p">(</span><span class="n">event</span><span class="o">.</span><span class="n">key</span> <span class="ow">in</span> <span class="p">[</span><span class="n">K_ESCAPE</span><span class="p">,</span> <span class="n">K_q</span><span class="p">]):</span>
                <span class="n">running</span> <span class="o">=</span> <span class="bp">False</span>

        <span class="n">mpos</span> <span class="o">=</span> <span class="n">pygame</span><span class="o">.</span><span class="n">mouse</span><span class="o">.</span><span class="n">get_pos</span><span class="p">()</span>
        <span class="n">p</span> <span class="o">=</span> <span class="n">from_pygame</span><span class="p">(</span> <span class="n">Vec2d</span><span class="p">(</span><span class="n">mpos</span><span class="p">),</span> <span class="n">screen</span> <span class="p">)</span>
        <span class="n">mouse_position</span> <span class="o">=</span> <span class="n">p</span>
        <span class="n">cannon_body</span><span class="o">.</span><span class="n">angle</span> <span class="o">=</span> <span class="p">(</span><span class="n">mouse_position</span> <span class="o">-</span> <span class="n">cannon_body</span><span class="o">.</span><span class="n">position</span><span class="p">)</span><span class="o">.</span><span class="n">angle</span>

        <span class="c">### Clear screen</span>
        <span class="n">screen</span><span class="o">.</span><span class="n">fill</span><span class="p">(</span><span class="n">pygame</span><span class="o">.</span><span class="n">color</span><span class="o">.</span><span class="n">THECOLORS</span><span class="p">[</span><span class="s">&quot;black&quot;</span><span class="p">])</span>

        <span class="c">### Draw stuff</span>
        <span class="n">draw_space</span><span class="p">(</span><span class="n">screen</span><span class="p">,</span> <span class="n">space</span><span class="p">)</span>

        <span class="c">### Update physics</span>
        <span class="n">fps</span> <span class="o">=</span> <span class="mi">60</span>
        <span class="n">dt</span> <span class="o">=</span> <span class="mf">1.</span><span class="o">/</span><span class="n">fps</span>
        <span class="n">space</span><span class="o">.</span><span class="n">step</span><span class="p">(</span><span class="n">dt</span><span class="p">)</span>

        <span class="c">### Info and flip screen</span>
        <span class="n">screen</span><span class="o">.</span><span class="n">blit</span><span class="p">(</span><span class="n">font</span><span class="o">.</span><span class="n">render</span><span class="p">(</span><span class="s">&quot;fps: &quot;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">clock</span><span class="o">.</span><span class="n">get_fps</span><span class="p">()),</span> <span class="mi">1</span><span class="p">,</span> <span class="n">THECOLORS</span><span class="p">[</span><span class="s">&quot;white&quot;</span><span class="p">]),</span> <span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">))</span>
        <span class="n">screen</span><span class="o">.</span><span class="n">blit</span><span class="p">(</span><span class="n">font</span><span class="o">.</span><span class="n">render</span><span class="p">(</span><span class="s">&quot;Aim with mouse, click to fire&quot;</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">THECOLORS</span><span class="p">[</span><span class="s">&quot;darkgrey&quot;</span><span class="p">]),</span> <span class="p">(</span><span class="mi">5</span><span class="p">,</span><span class="n">height</span> <span class="o">-</span> <span class="mi">35</span><span class="p">))</span>
        <span class="n">screen</span><span class="o">.</span><span class="n">blit</span><span class="p">(</span><span class="n">font</span><span class="o">.</span><span class="n">render</span><span class="p">(</span><span class="s">&quot;Press R to reset, ESC or Q to quit&quot;</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">THECOLORS</span><span class="p">[</span><span class="s">&quot;darkgrey&quot;</span><span class="p">]),</span> <span class="p">(</span><span class="mi">5</span><span class="p">,</span><span class="n">height</span> <span class="o">-</span> <span class="mi">20</span><span class="p">))</span>

        <span class="n">pygame</span><span class="o">.</span><span class="n">display</span><span class="o">.</span><span class="n">flip</span><span class="p">()</span>
        <span class="n">clock</span><span class="o">.</span><span class="n">tick</span><span class="p">(</span><span class="n">fps</span><span class="p">)</span>

<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">&#39;__main__&#39;</span><span class="p">:</span>
    <span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="n">main</span><span class="p">())</span>
</pre></div>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="../index.html">
              <img class="logo" src="../_static/pymunk_logo_sphinx.png" alt="Logo"/>
            </a></p>
<h3><a href="../index.html">Table Of Contents</a></h3>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="../readme.html">Readme</a></li>
<li class="toctree-l1"><a class="reference internal" href="../installation.html">Installation</a></li>
<li class="toctree-l1"><a class="reference internal" href="../pymunk.html">API Reference</a></li>
<li class="toctree-l1"><a class="reference internal" href="../examples.html">Examples</a></li>
<li class="toctree-l1 current"><a class="reference internal" href="../tutorials.html">Tutorials</a><ul class="current">
<li class="toctree-l2 current"><a class="current reference internal" href="">Arrows and other pointy sticky things</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#before-we-start">Before we start</a></li>
<li class="toctree-l3"><a class="reference internal" href="#basic-scene">Basic scene</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="SlideAndPinJoint.html">Slide and Pin Joint Demo Step by Step</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../advanced.html">Advanced</a></li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="../tutorials.html"
                        title="previous chapter">Tutorials</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="SlideAndPinJoint.html"
                        title="next chapter">Slide and Pin Joint Demo Step by Step</a></p>
  <h3>This Page</h3>
  <ul class="this-page-menu">
    <li><a href="../_sources/tutorials/Arrows.txt"
           rel="nofollow">Show Source</a></li>
  </ul>
<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="SlideAndPinJoint.html" title="Slide and Pin Joint Demo Step by Step"
             >next</a> |</li>
        <li class="right" >
          <a href="../tutorials.html" title="Tutorials"
             >previous</a> |</li>
        <li><a href="../index.html">pymunk 3.0.0 documentation</a> &raquo;</li>
          <li><a href="../tutorials.html" >Tutorials</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
        &copy; Copyright 2012, Victor Blomqvist.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3.
    </div>
  </body>
</html>